// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Tool from "../../framework/Tool";

const {ccclass, property} = cc._decorator;

@ccclass
export default class moveBtn extends cc.Component {

    initData = null;

    initPos = null;

    onLoad () {
        this.node.on("touchstart",this.TouchStart,this);
        this.node.on("touchmove",this.TouchMove,this);
        this.node.on("touchend",this.TouchEnd,this);
        this.node.on("touchcancel",this.TouchEnd,this);
    }

    TouchStart(event){
        this.initData = new Date().getTime();
        this.initPos = this.node.convertToNodeSpaceAR(event.getLocation());
    }

    TouchMove(event){
        let delay = (new Date().getTime() - this.initData) / 1000;
        if(delay > 3){
            let pos = this.node.convertToNodeSpaceAR(event.getLocation());
            let pos1 = pos.sub(this.initPos).normalize();
            this.node.x += pos1.x * 5;
            this.node.y += pos1.y * 5;
        }else{
            return;
        }
    }

    TouchEnd(event){
        let delay = (new Date().getTime() - this.initData) / 1000;
        if(delay > 3){
            console.log("出现碎片弹框");
            Tool.loadPrefab("prefabs/getSkin",res=>{
                let view = cc.instantiate(res);
                view.getComponent("getSkinMediator").setCallBack(false,2);
                view.parent = cc.find("Canvas");
            });
        }else{
            console.log("播放视频");
        }
    }
}
